package com.bootdo.collection.service.impl;

import com.bootdo.collection.dao.PictureSetDao;
import com.bootdo.collection.domain.PictureSetDO;
import com.bootdo.collection.domain.SourceStationDO;
import com.bootdo.collection.service.PictureSetService;
import com.bootdo.collection.service.SourceStationService;
import com.bootdo.collection.utils.RecursionUtil;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.huaban.analysis.jieba.JiebaSegmenter;
import com.huaban.analysis.jieba.SegToken;
import org.apache.catalina.util.URLEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;


@Service
public class PictureSetServiceImpl implements PictureSetService {
    @Autowired
    private PictureSetDao pictureSetDao;
    @Autowired
    private SourceStationService sourceStationService;
    @Autowired
    private RecursionUtil recursionUtil;
    public PageUtils list(Map<String, Object> params) {
        int currentPage = Integer.parseInt((String) params.get("currentPage"));
        int pageSize = Integer.parseInt((String) params.get("pageSize"));
        Integer id = Integer.parseInt((String) params.get("id"));
        SourceStationDO sourceStationDO = sourceStationService.get(id);
        String programName = sourceStationDO.getName();
        if (sourceStationDO.getParentId() == 0) {
            params.put("stationId", sourceStationDO.getId());
            params.put("programName","站点,不查找");
        } else {
            //得到该栏目的站点
            sourceStationDO = recursionUtil.recursion(sourceStationDO);
            //得到该栏目和子栏目
            List<String> allSubName = sourceStationService.getSubName(id);
            params.put("programName", allSubName);
            params.put("stationId", sourceStationDO.getId());
        }
        PageHelper.startPage(currentPage, pageSize);
        List<PictureSetDO> allPictureSet = pictureSetDao.list(params);
        int countNums = pictureSetDao.total(params);
        PageUtils pageUtils = new PageUtils(allPictureSet,countNums);
        return pageUtils;
    }

    @Override
    public PictureSetDO getPictureSetDOById(int pictureSetId) {
        return pictureSetDao.getPictureSetDOById(pictureSetId);
    }

    @Override
    public int updateCount(PictureSetDO pictureSetDO) {
        return pictureSetDao.updateCount(pictureSetDO);
    }

    /**
     * 模糊查询或分词查询
     * @param params
     * @return PageUtils
     */
    @Override
    public PageUtils getListByTitle(Map<String,Object> params) {
        if(params.get("id") != null && Integer.parseInt((String) params.get("id")) != -1){
            Integer id = Integer.parseInt((String)params.get("id") );
            SourceStationDO sourceStationDO = sourceStationService.get(id);
            if (sourceStationDO.getParentId() == 0) {
                params.put("stationId", sourceStationDO.getId());
            }else{
                //查找该栏目下的站点
                sourceStationDO = recursionUtil.recursion(sourceStationDO);
                params.put("stationId", sourceStationDO.getId());
                //得到该栏目和子栏目
                List<String> allSubName = sourceStationService.getSubName(id);
                params.put("programName", allSubName);
            }
        }
        //处理参数问题
        int currentPage = Integer.parseInt((String) params.get("currentPage"));
        int pageSize = Integer.parseInt((String) params.get("pageSize"));
        String title = (String) params.get("title");
        //分词或模糊查询标记
        String isParticiple = (String) params.get("isParticiple");
        //0,不分词，模糊查询
        if("0".equals(isParticiple)){
            PageHelper.startPage(currentPage, pageSize);
            List<PictureSetDO> allPictureSet = pictureSetDao.fuzzyQuery(params);
            int total = pictureSetDao.fuzzyQueryTotal(params);
            return new PageUtils(allPictureSet,total);
        }else {
            JiebaSegmenter segmenter = new JiebaSegmenter();
            List<SegToken> list = segmenter.process(title, JiebaSegmenter.SegMode.INDEX);
            StringBuilder stringBuilder = new StringBuilder();
            for(SegToken segToken:list){
                if(segToken.word.trim().length() == 0){
                    continue;
                }
                String wordUrlEncode = URLEncoder.DEFAULT.encode(segToken.word, Charset.defaultCharset());
                wordUrlEncode = StringUtils.replaceAll(wordUrlEncode,"%","");
                stringBuilder.append("+" + wordUrlEncode);
            }
            stringBuilder.deleteCharAt(0);
            params.put("titleParticiple",stringBuilder.toString());
            PageHelper.startPage(currentPage, pageSize);
            List<PictureSetDO> allPictureSet = pictureSetDao.getListByTitle(params);
            int total = pictureSetDao.getTotalByTitle(params);
            return  new PageUtils(allPictureSet,total);
        }
    }

    @Override
    public int updateUploadStationName(String uploadStaionName, int pictureSetId) {
        String oldUploadStationName = pictureSetDao.getUploadStationName(pictureSetId);
        if(StringUtils.isNotBlank(oldUploadStationName)){
            uploadStaionName = uploadStaionName + "&" + oldUploadStationName;
        }
        return pictureSetDao.updateUploadStationName(uploadStaionName,pictureSetId);
    }

    /**
     * 显示缩略图
     * @param allPictureSet
     */
//    public void initPictureSet(List<PictureSetDO> allPictureSet){
//         List<Future> futureList = new ArrayList<>();
//         for(PictureSetDO pictureSetDO:allPictureSet){
//             if(StringUtils.isNotBlank(sourceStationService.get(pictureSetDO.getSourceStationId()).getRefer())){
//             List<PictureDO> pictureDOS = pictureSetDO.getPictures();
//                 for(PictureDO pictureDO: pictureDOS){
//                     futureList.add(executor.submit(new ReplacePicUrlToBase64Util(pictureDO)));
//                 }
//             }
//         }
//         for(Future future:futureList){
//             try {
//                 future.get();
//             } catch (InterruptedException e) {
//                 e.printStackTrace();
//             } catch (ExecutionException e) {
//                 e.printStackTrace();
//             }
//         }
//     }


}
